﻿using System.Linq;
using Eagles.Base.DataBase;
using Eagles.DomainService.Model.Sms;
using Eagles.Interface.DataAccess.SmsAccess;

namespace Ealges.DomianService.DataAccess.SmsData
{
    public class SmsDataAccess : ISmsAccess
    {
        private readonly IDbManager dbManager;

        public SmsDataAccess(IDbManager dbManager)
        {
            this.dbManager = dbManager;
        }

        public TbOrgSmsConfigExt GetSmsConfig(int appId)
        {
            var result = dbManager.Query<TbOrgSmsConfigExt>(@"select a.VendorId,b.VendorName,a.MaxCount,a.SendCount,b.AppKey,b.SginKey from eagles.tb_org_sms_config a 
inner join eagles.tb_sms_config b on a.vendorId = b.vendorId where a.Status = 0 and b.Status = 0 and a.Orgid = @Orgid order by a.Priority desc ", new {OrgId = appId});
            if (result != null && result.Any())
                return result.FirstOrDefault();
            return null;
        }

        public int CreateSmsSendLog(TbSmsSendLog smsSendLog)
        {
            //增加修改积分的流水
            return dbManager.Excuted(@"insert into eagles.tb_sms_send_log (OrgId,TraceId,VendorId,SmsContent,CreateTime,Phone,Status,RequestMsg,ResponseMsg) values 
(@OrgId,@TraceId,@VendorId,@SmsContent,@CreateTime,@Phone,@Status,@RequestMsg,@ResponseMsg) ", smsSendLog);
        }

        public int AddSendCount(int appId, int vendorId)
        {
            return dbManager.Excuted("update eagles.tb_org_sms_config set SendCount = ifnull(SendCount,0) + 1 where OrgId=@OrgId and VendorId=@VendorId ",
                new {OrgId = appId, VendorId = vendorId});
        }
    }
}